const JWT = require('jsonwebtoken');
const GROUP_NAME = 'users';
let users = {
    method: 'POST',
    path: `/${GROUP_NAME}/createJWT`,
    handler: async (request, reply) => {
        try {
            const generateJWT = (jwtInfo) => {
                const payload = {
                    userId: jwtInfo.userId,
                    exp: Math.floor(new Date().getTime() / 1000) + 7 * 24 * 60 * 60 // 失效时间
                };
                return JWT.sign(payload, process.env.JWT_SECRET);
            };
            reply(generateJWT({
                userId: 1
            }));
        } catch (error) {
            console.log(error)
        }
    },
    config: {
        tags: ['api', GROUP_NAME],
        description: '用于测试的用户 JWT 签发',
        auth: false // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
    }
};
module.exports = [users];
